home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / drgnsmth.cpt / Dragonsmith 1.1 / Base files / Classes / AppleEventQueue.c next >
Encoding:
C/C++ Source or Header  |  1992-10-10  |  1.9 KB  |  81 lines

  1. /*
  2.     AppleEventQueue.c
  3.     
  4.     A simple queue used to keep track of suspended Apple events
  5.     
  6.     Created:    02 Sep 1992    Constructor, Put, Get ╤ add a destructor later? (it should call an error-returning handler)
  7.     Modified:    
  8.     
  9.     Copyright ⌐ 1992 by Paul M. Hoffman
  10.     Send comments or suggestions to paul.hoffman@um.cc.umich.edu
  11.     
  12.     This source code may be freely used, altered, and distributed in any way as long as:
  13.         1.    It is GIVEN away rather than sold (except as expressly permitted by the author)
  14.         2.    This statement and the above copyright notice are left intact.
  15.  
  16. */
  17.  
  18. #include    "AppleEventQueue.h"
  19.  
  20. AppleEventQueue::AppleEventQueue (void)
  21. {
  22.     head = tail = NULL;
  23.     numAEvents = 0;
  24. }
  25.  
  26. OSErr AppleEventQueue::Put (AppleEvent *event, AppleEvent *reply, AEHandlerFunc handler, long refcon)
  27. {
  28.     // Add an Apple event and its associated data to end the queue
  29.     
  30.     PendingAERec    **h;
  31.     
  32.     h = (PendingAERec **) NewHandle (sizeof (PendingAERec));
  33.     if (h == NULL)
  34.         return memFullErr;
  35.         
  36.     numAEvents++;
  37.     
  38.     (*h)->event = *event;
  39.     (*h)->reply = *reply;
  40.     (*h)->handler = handler;
  41.     (*h)->refcon = refcon;
  42.     (*h)->next = NULL;
  43.     
  44.     if (head == NULL)            // If the queue is empty, set head = the new entry
  45.         head = h;
  46.     else                        // Otherwise, set 
  47.         (*tail)->next = h;
  48.     tail = h;                    // Insertion is always done at the tail
  49.     
  50.     return noErr;
  51. }
  52.  
  53. OSErr AppleEventQueue::Get (AppleEvent *event, AppleEvent *reply, AEHandlerFunc *handler, long *refcon)
  54. {
  55.     // Get an apple event and its associated data from the front of the queue
  56.  
  57.     PendingAERec    **h;
  58.     
  59.     if (numAEvents <= 0)                // Make a sanity check ╤ we don't want to return a bogus function address!
  60.         return noOutstandingHLE;
  61.     
  62.     h = head;
  63.     if (h == NULL) {                        // One more sanity check
  64.         numAEvents = 0;
  65.         return noOutstandingHLE;
  66.     }
  67.     
  68.     head = (*h)->next;
  69.     if (--numAEvents == 0)
  70.         tail = NULL;
  71.         
  72.     // Return values
  73.     *event = (*h)->event;
  74.     *reply = (*h)->reply;
  75.     *handler = (*h)->handler;
  76.     *refcon = (*h)->refcon;
  77.     
  78.     DisposHandle ((Handle) h);
  79. }
  80.  
  81.